﻿#ifndef ESOBJECTHEADER_H
#define ESOBJECTHEADER_H

#include <QDebug>
#include <QMetaType>
#include <QObject>
#include <QString>

#include "DObjectMgr.h"

template <typename _interface, typename _concret = _interface>
class DObjectPtr final
{
public:
    explicit DObjectPtr(int64_t nID = ObjectDefaultID) { t = getPointer(nID); }
    virtual ~DObjectPtr() = default;

    _concret*& operator->()
    {
        if (nullptr == t)
        {
            qDebug() << "->NULL ptr";
        }
        return t;
    }

    operator QObject*()
    {
        if (nullptr == t)
        {
            qDebug() << "->NULL ptr";
        }
        return dynamic_cast<QObject*>(t);
    }

    static _concret* getPointer(int64_t nID = ObjectDefaultID)
    {
        ObjectID<_interface> objID(dGetClassName<_interface>(), nID);
        return dynamic_cast<_concret*>(g_objMgr.getObj<_interface>(objID));
    }

    operator bool() { return t != nullptr; }

private:
    DObjectPtr(const DObjectPtr&) = delete;
    DObjectPtr& operator=(DObjectPtr&) = delete;

    bool operator==(const DObjectPtr& ptr) = delete;
    bool operator!=(const DObjectPtr& ptr) = delete;

private:
    _concret* t = Q_NULLPTR;
};

#endif
